// 示例 1：

// 输入：nums = [1, 2, 2]
// 输出：[[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
// 示例 2：

// 输入：nums = [0]
// 输出：[[], [0]]

nums = [1, 2, 2]

var subsetsWithDup = function (nums) {
    let result = [], path = []
    let len = nums.length
    let used = new Array(len).fill(false)
    nums.sort()

    function dfs(index) {
        result.push(path.slice())
        for (let i = index; i < len; i++) {
            if (i > 0 && nums[i] === nums[i - 1] && used[i - 1] === false) {
                continue
            }
            path.push(nums[i])
            used[i] = true
            dfs(i + 1)
            path.pop()
            used[i] = false
        }
    }
    dfs(0)
    return result
};

console.log(subsetsWithDup(nums));
